﻿#include "CRobBattery.h"

CRobBattery::CRobBattery(QWidget *parent)
	: CBasicBattery(parent)
{
}

void CRobBattery::paintEvent(QPaintEvent *event)
{
	QPainter* pPainter = new QPainter(this);
	//创建画家对象
	drawBorder(pPainter);
	drawFill(pPainter);
	delete pPainter;
}

void CRobBattery::drawBorder(QPainter *painter)
{
	// 保存当前画笔状态
	painter->save();

	// 设置画笔颜色和样式
	painter->setPen(this->m_border);

	// 绘制电池外壳，采用圆角矩形
	painter->drawRoundedRect(0, 0, m_width * 0.93, this->m_height*0.93, this->m_roundPresent, this->m_roundPresent);

	// 计算并绘制电池右侧的正极
	int poleWidth = 5; // 正极宽度
	int poleHeight = m_height * 0.3; // 正极高度
	int poleX = m_width * 0.93 + m_offset.x(); // 正极横坐标，位于电池右侧边框处并考虑间隔
	int poleY = (m_height - poleHeight) / 2; // 正极纵坐标，使其垂直居中

	// 设置正极颜色与边框颜色一致，并绘制正极
	painter->setBrush(this->m_border);
	painter->drawRect(poleX, poleY, poleWidth, poleHeight);

	// 恢复之前保存的画笔状态
	painter->restore();
}

void CRobBattery::drawFill(QPainter *painter)
{
	painter->save();

	// 计算电池内部填充区域的宽度
	qreal fillWidth = (m_width * 0.93 - 2 * m_offset.x()) * m_level / 100;
	// 计算电池内部填充区域的高度
	qreal fillHeight = m_height*0.93 - 2 * m_offset.y();

	// 计算电池内部填充区域的起始点
	qreal fillX = m_offset.x();
	qreal fillY = m_offset.y();

	// 根据电池状态设置填充颜色
	QColor fillColor = m_padding;
	if (m_status == Charging) {
		fillColor = m_charge;
	}
	else if (m_level <= 20) {
		fillColor = m_nobattery;
	}

	// 设置填充颜色
	painter->setBrush(fillColor);

	// 绘制填充区域
	painter->drawRoundedRect(QRectF(fillX, fillY, fillWidth, fillHeight), this->m_roundPresent, this->m_roundPresent);

	painter->restore();
}

CRobBattery::~CRobBattery()
{}
